/**
 * 支付管理 支付配置
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>支付配置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.cnfName" placeholder="输入名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select size="small" v-model="formInline.mode" placeholder="请选择">
          <el-option v-for="type in mode" :label="type.name" :value="type.id" :key="type.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.merchantCode" placeholder="输入商户号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="this.opRight.includes('save')" size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="index" width="60">
      </el-table-column>
      <el-table-column sortable prop="cnfName" label="名称" width="200" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="name" label="支付类型" width="120" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="merchantCode" label="商户号" width="100" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="subCode" label="微信子商户" width="140" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="appId" label="应用ID" width="100" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="encryptionMode" label="加密类型" width="120" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="secretKey" label="商户签名密钥" width="180" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="callbackInfo" label="通知回调" width="140" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="merchantName" label="卖家" width="150" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column sortable prop="certPath" label="微信证书路径" width="150" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button v-if="opRight.includes('save')" size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button v-if="opRight.includes('delete')" size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog('editForm')">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="名称" prop="name">
          <el-input size="small" v-model="editForm.cnfName" auto-complete="off" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="支付类型" prop="mode">
          <el-select size="small" v-model="editForm.mode" placeholder="请选择支付类型" class="userRole">
            <el-option v-for="type in mode" :label="type.name" :value="type.id" :key="type.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户号" prop="partner">
          <el-input size="small" v-model="editForm.merchantCode" auto-complete="off" placeholder="请输入商户号"></el-input>
        </el-form-item>
        <el-form-item label="微信子商户" prop="subCode">
          <el-input size="small" v-model="editForm.subCode" auto-complete="off" placeholder="请输入微信子商户"></el-input>
        </el-form-item>
        <el-form-item label="应用ID" prop="appId">
          <el-input size="small" v-model="editForm.appId" auto-complete="off" placeholder="请输入应用ID"></el-input>
        </el-form-item>
        <el-form-item label="通知回调" prop="callbackInfo">
          <el-input size="small" v-model="editForm.callbackInfo" auto-complete="off" placeholder="请输入通知回调"></el-input>
        </el-form-item>
        <el-form-item label="加密类型" prop="encryptionMode">
          <el-input size="small" v-model="editForm.encryptionMode" auto-complete="off" placeholder="请输入加密类型"></el-input>
        </el-form-item>
        <el-form-item label="商户签名密钥" prop="secretKey">
          <el-input size="small" v-model="editForm.secretKey" auto-complete="off" placeholder="请输入商户签名密钥"></el-input>
        </el-form-item>
        <el-form-item label="支付宝卖家" prop="merchantName">
          <el-input size="small" v-model="editForm.merchantName" auto-complete="off" placeholder="请输入支付宝卖家"></el-input>
        </el-form-item>
        <el-form-item label="微信证书路径" prop="certPath">
          <el-input size="small" v-model="editForm.certPath" auto-complete="off" placeholder="请输入微信证书路径"></el-input>
        </el-form-item>
        <el-form-item label="微信证书密码" prop="certPassword">
          <el-input size="small" v-model="editForm.certPassword" auto-complete="off" placeholder="请输入微信证书密码"></el-input>
        </el-form-item>
        <el-form-item label="支付宝私钥" prop="rsaKey">
          <el-input size="small" v-model="editForm.rsaKey" auto-complete="off" placeholder="请输入支付宝私钥"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('editForm')">取消</el-button>
        <el-button v-if="opRight.includes('save')" size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {ConfigList, ConfigSave, ConfigDelete, ModeList} from '../../api/payMG'
import Pagination from '../../components/Pagination'
  import {UserOpRight} from "../../api/userMG";
  import {lert} from "../../api/axiosFun";
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      mode: [],
      editForm: {
        issave: false,
        id: '',
        cnfName: '',
        mode: 1,
        merchantCode: '',
        subCode: '',
        appId: '',
        callbackInfo: '',
        encryptionMode: '',
        secretKey: '',
        merchantName: '',
        certPath: '',
        certPassword: '',
        rsaKey: '',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        cnfName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        mode: [
          { required: true, message: '请选择支付方式', trigger: 'blur' }
        ],
        merchantCode: [{ required: true, message: '请输入商户号', trigger: 'blur' }],
        subCode: [
          { required: true, message: '请输入微信子商户号', trigger: 'blur' }
        ],
        appId: [{ required: true, message: '请输入应用ID', trigger: 'blur' }],
        callbackInfo: [
          { required: true, message: '请输入通知回调', trigger: 'blur' }
        ],
        encryptionMode: [
          { required: true, message: '请输入加密类型', trigger: 'blur' }
        ],
        secretKey: [
          { required: true, message: '请输入商户签名密钥', trigger: 'blur' }
        ],
        merchantName: [
          { required: true, message: '请输入卖家名称', trigger: 'blur' }
        ],
        certPath: [
          { required: true, message: '请输入证书路径', trigger: 'blur' }
        ],
        certPassword: [
          { required: true, message: '请输入证书密码', trigger: 'blur' }
        ],
        rsaKey: [
          { required: true, message: '请输入支付宝私钥', trigger: 'blur' }
        ]
      },
      formInline: {
        cnfName:'',
        page: 1,
        limit: 10,
        mode: '',
        merchantCode: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      opRight: [],
      login:{
        code: 'cnf',
        token:localStorage.getItem('userdata')
      },
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getMode()
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    //获取支付方式信息
    getMode(){
      ModeList()
       .then(res => {
              lert(res);
          this.mode=JSON.parse(res.data)
        });
    },
    // 获取公司列表
    getdata(parameter) {
      this.loading = true
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      //获取当前页面的操作权限(opRight)
      UserOpRight(this.login)
        .then(res=>{
          this.opRight = JSON.parse(res.data);
        })
      ConfigList(parameter)
       .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.loading = false
            var result = JSON.parse(res.data);
            this.listData =result.records
            // 分页赋值
            this.pageparm.currentPage = result.current
            this.pageparm.pageSize = result.size
            this.pageparm.total = result.total
            this.$message.success(res.msg)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
  //         {
      //       addUser: null,
      //       editUser: null,
      //       addTime: null,
      //       editTime: null,
      //       id: 11,
      //       name: '-微信301',
      //       mode: 2,
      //       partner: '1271942301',
      //       subCode: '1357972202',
      //       appId: 'wx3ef7713adf0a97b8',
      //       callbackInfo:
      //         'http://180.166.211.210:8114/machine-pay-consumer/pay/wx/notify',
      //       encryptionMode: 'NATIVE',
      //       secretKey: '2e3cdaf5aa051c16563c0b8916184d5d',
      //       merchantName: null,
      //       certPath:
      //         '/usr/local/tomcat_provider/webapps/machine-service-provider-0.0.1-SNAPSHOT/conf/apiclient_cert_1271942301.p12',
      //       certPassword: '1271942301',
      //       rsaKey: null,
      //       deptId: null
      //     }
      //   ]
      // }
      // this.loading = false
      // this.listData = res.data
      // // 分页赋值
      // this.pageparm.currentPage = this.formInline.page
      // this.pageparm.pageSize = this.formInline.limit
      // this.pageparm.total = res.count
      // // 模拟数据结束


    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.issave=false
        this.editForm.id = row.id
        this.editForm.cnfName = row.cnfName
        this.editForm.mode = row.mode
        this.editForm.merchantCode = row.merchantCode
        this.editForm.subCode = row.subCode
        this.editForm.appId = row.appId
        this.editForm.callbackInfo = row.callbackInfo
        this.editForm.encryptionMode = row.encryptionMode
        this.editForm.secretKey = row.secretKey
        this.editForm.merchantName = row.merchantName
        this.editForm.certPath = row.certPath
        this.editForm.certPassword = row.certPassword
        this.editForm.rsaKey = row.rsaKey
      } else {
        this.title = '添加'
        this.editForm.issave=true
        this.editForm.id = ''
        this.editForm.cnfName = ''
        this.editForm.mode = ''
        this.editForm.merchantCode = ''
        this.editForm.subCode = ''
        this.editForm.appId = ''
        this.editForm.callbackInfo = ''
        this.editForm.encryptionMode = ''
        this.editForm.secretKey = ''
        this.editForm.merchantName = ''
        this.editForm.certPath = ''
        this.editForm.certPassword = ''
        this.editForm.rsaKey = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          ConfigSave(this.editForm)
           .then(res => {
              lert(res);
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '支付配置保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('支付配置信息保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除支付配置
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ConfigDelete(row.id)
           .then(res => {
              lert(res);
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '支付配置已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('支付配置信息删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog(formName) {
      this.editFormVisible = false
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>


